package 优先算法.位运算.判定字符是否唯一;

/**
 * https://leetcode.cn/problems/missing-number/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        String s = s = "leetcode";
        System.out.println(so.isUnique(s));
    }
}
class Solution {
    //位图思想
    public boolean isUnique(String astr) {
        int Byte = 0;
        if(astr.length()>26){
            return false;
        }
        for(int i=0; i < astr.length(); i++){
            int index = (int) astr.charAt(i) - 'a';
            if(((Byte >> index) & 1 ) == 1){//位图右边从零开始正好右移index
                return false;
            }else{
                Byte = Byte | (1<<index);
            }
        }
        return true;
    }
}
class Solution1 {
    //哈希表
    public boolean isUnique(String astr) {
        int[] arr = new int[26];
        if(astr.length()>26){
            return false;
        }
        for(int i=0; i < astr.length(); i++){
            char c = astr.charAt(i);
            if(arr[c-'a'] == 1){
                return false;
            }else{
                arr[c-'a'] = 1;
            }
        }
        return true;
    }
}